home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Technology Seed / Mac Tech Seed Jul '96 / Mac Tech Seed Jul '96.toast / Walkabout a3 / SDK (for Developers) / WA Task ƒ / WATask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-30  |  8.7 KB  |  310 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        WATask.c
  3.  
  4.     Contains:    a generic task that will provide developers with a starting point
  5.                 for writing their own widgets.
  6.  
  7.     Written by:    Eric Slosser
  8.  
  9.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13. */
  14.  
  15. #include <strings.h>
  16. #include <stdio.h>
  17.  
  18. #include <Components.h>
  19.  
  20. #include "WalkaboutComponents.h"
  21.  
  22. #define DEBUGLEVEL DEBUGFULL
  23. #include "Exceptions.h"
  24.  
  25. //-------------------------------------------------------------------------------------
  26. //    A little cruftiness to allow us to compile under different environments
  27.  
  28. #if !COMPILING_STANDALONE
  29.     #define    WAEnterCodeResource()
  30.     #define    WAExitCodeResource()
  31.     #define WAPrepareCodeEntry()
  32. #else
  33.     #ifdef THINK_C
  34.         #include <SetupA4.h>
  35.         #define WAPrepareCodeEntry()    RememberA0()
  36.         #define    WAEnterCodeResource()    { SetUpA4(); }
  37.         #define    WAExitCodeResource()    { RestoreA4(); }
  38.     #else
  39.     #ifdef __MWERKS__
  40.         #include <A4Stuff.h>
  41.         #include <SetupA4.h>
  42.         #define WAPrepareCodeEntry()     // PrepareCallBack()
  43.         #define    WAEnterCodeResource()    { EnterCodeResource()
  44.         #define    WAExitCodeResource()    ExitCodeResource(); }
  45.     #else
  46.     #ifdef __SC__        // MPW
  47.         #define WAPrepareCodeEntry()        GlobalWorld     saved = GetCurrentGlobalWorld()
  48.         #define    WAEnterCodeResource()        InitCodeResource()
  49.         #define    WAExitCodeResource()        SetCurrentGlobalWorld(saved); FreeGlobalWorld()
  50.     #endif
  51.     #endif
  52.     #endif
  53. #endif
  54.  
  55. //-------------------------------------------------------------------------------------
  56. // task specific defines
  57.  
  58. #define    kTaskVersion            0
  59.  
  60. //-------------------------------------------------------------------------------------
  61.  
  62. typedef struct  {                // This is what's contained in the handle that we 
  63.                                 // pass back to the shell.
  64.     Boolean        installed;
  65.     Boolean        active;
  66.     
  67.     } SettingsRec, **SettingsHandle;
  68.     
  69. struct Globals {                    // globals for this task
  70.     Component    self;
  71.     short        refNum;        // the component's resource fork
  72.     };
  73. typedef struct Globals Globals, **GlobalsHdl;
  74.  
  75. //-------------------------------------------------------------------------------------
  76. #pragma mark -
  77. //-------------------------------------------------------------------------------------
  78. static OSErr    UseSetting( SettingsHandle s, WARebootFlags *flags );
  79. static OSErr    UseSetting( SettingsHandle s, WARebootFlags *flags )
  80. {
  81.     OSErr            err = noErr;
  82.  
  83.     *flags = walkAvailableNow;
  84.  
  85.     return (err);    
  86. }    // UseSetting
  87.  
  88. //-------------------------------------------------------------------------------------
  89. static OSErr    GetCurrentSetting(SettingsHandle s);
  90. static OSErr    GetCurrentSetting(SettingsHandle s)
  91. {
  92.     OSErr            err = noErr;
  93.  
  94.     SetHandleSize((Handle) s, sizeof(SettingsRec));
  95.     if ( err=MemError() ) goto BAIL;
  96.     
  97.     // do your getting here.
  98.     
  99. BAIL:
  100.     return (err);
  101. }    // GetCurrentSetting
  102.  
  103.  
  104. //-------------------------------------------------------------------------------------
  105. //    Compare the two settings.  Depending on what you store in a SettingsRec, your
  106. //        comparision may be more complex.
  107.  
  108. static Boolean    EqualSettings(SettingsRec *s1, SettingsRec *s2);
  109. static Boolean    EqualSettings(SettingsRec *s1, SettingsRec *s2)
  110. {
  111.     return ( s1->installed     == s2->installed &&
  112.              s1->active     == s2->active );
  113. }
  114.  
  115. //-------------------------------------------------------------------------------------
  116. #pragma mark -        The rest of the routines are boilerplate for all widgets.
  117. //-------------------------------------------------------------------------------------
  118. static pascal ComponentResult    GetCurrent( SettingsHandle s );
  119. static pascal ComponentResult    GetCurrent( SettingsHandle s )
  120. {
  121.     OSErr        err;
  122.     
  123.     err = GetCurrentSetting( s );
  124.     
  125.     return err;
  126. }
  127.  
  128. //-------------------------------------------------------------------------------------
  129. static    pascal ComponentResult    SetCurrent( SettingsHandle new, WARebootFlags *flags);
  130. static    pascal ComponentResult    SetCurrent( SettingsHandle new, WARebootFlags *flags)
  131. {
  132.     ComponentResult    err = noErr;
  133.     SettingsHandle    cur = nil;
  134.     
  135.     *flags = walkNoChange;                        // preset to harmless value
  136.  
  137.     cur = (SettingsHandle) NewHandle( sizeof(SettingsRec) );
  138.     GetCurrentSetting( cur );
  139.     
  140.     if (!EqualSettings(*new,*cur))
  141.         {
  142.         err = UseSetting(new,flags);
  143.         }
  144.         
  145. BAIL:            
  146.     return err;
  147. }
  148. //-------------------------------------------------------------------------------------
  149. static pascal ComponentResult    CompareSettings( SettingsHandle setting1, SettingsHandle setting2, Boolean *equal );
  150. static pascal ComponentResult    CompareSettings( SettingsHandle setting1, SettingsHandle setting2, Boolean *equal )
  151. {
  152.     ComponentResult    err = noErr;
  153.     
  154.     *equal = EqualSettings( *setting1, *setting2 );
  155.  
  156.     return err;
  157. }
  158.  
  159. //-------------------------------------------------------------------------------------
  160. static    pascal ComponentResult    DescribeSettings( SettingsHandle s, StringHandle text);
  161. static    pascal ComponentResult    DescribeSettings( SettingsHandle s, StringHandle text)
  162. {
  163.     ComponentResult    err = noErr;
  164.     Str255    tmp = "\pdescription not implemented";    
  165.     
  166.     PtrToXHand( tmp+1, (Handle) text, *tmp );
  167.     
  168.     return err;
  169. }
  170. //-------------------------------------------------------------------------------------
  171. static pascal ComponentResult    DescribeError( OSErr lastErr, Str255 errStr );
  172. static pascal ComponentResult    DescribeError( OSErr lastErr, Str255 errStr )
  173. {
  174.     ComponentResult    err = noErr;
  175.     Str255            tmp = "\pWe don't describe errors yet";
  176.     
  177.     BlockMoveData(tmp, errStr, *tmp+1);
  178. BAIL:
  179.     return err;
  180. }
  181. //-------------------------------------------------------------------------------------
  182. static    pascal ComponentResult Open(ComponentInstance self);
  183. static    pascal ComponentResult Open(ComponentInstance self)
  184. {
  185.     OSErr            err = noErr;
  186.     GlobalsHdl        g = (GlobalsHdl) NewHandleClear(sizeof(Globals));
  187.  
  188.     // allocate your widget's storage (if you have any).
  189.     // in the main routine, you'll have to change the call "CallComponentFunction" to 
  190.     // "CallComponentFunctionWithStorage", and alter the parameters for all the pascal
  191.     // functions to start with a pointer to your storage.
  192.  
  193.     if (!g)    { err = memFullErr; goto BAIL; }
  194.     SetComponentInstanceStorage(self,(Handle) g);
  195.  
  196.     (**g).self = (Component) self;
  197.     (**g).refNum = OpenComponentResFile( (Component) self );
  198.     
  199. BAIL:
  200.     return err;
  201. }    
  202. //-------------------------------------------------------------------------------------
  203. static    pascal ComponentResult Close(ComponentInstance self);
  204. static    pascal ComponentResult Close(ComponentInstance self)
  205. {
  206.     OSErr            err = noErr;
  207.     GlobalsHdl        g = (GlobalsHdl) GetComponentInstanceStorage(self);
  208.  
  209.     require( g, BAIL );
  210.     if ((**g).refNum>0)        
  211.         CloseComponentResFile((**g).refNum);
  212.  
  213.     DisposeHandle((Handle)g);
  214.     SetComponentInstanceStorage(self, nil);
  215.     
  216. BAIL:
  217.     return err;
  218. }
  219.  
  220. //-------------------------------------------------------------------------------------
  221. #if TASKS_IN_APP
  222. pascal    ComponentResult    WATask( ComponentParameters *params, Handle storage );
  223. pascal    ComponentResult    WATask( ComponentParameters *params, Handle storage )
  224. #else
  225. pascal    ComponentResult    main( ComponentParameters *params, Handle storage )
  226. #endif
  227. {
  228.     ComponentResult    result = noErr;
  229.     ComponentFunctionUPP     func = nil;
  230.     
  231.     WAPrepareCodeEntry();
  232.     WAEnterCodeResource();
  233.  
  234.     if (params->what<0)        // required request codes
  235.         {
  236.         switch (params->what)
  237.             {
  238.             case kComponentOpenSelect :
  239.                     func = (ComponentFunctionUPP) Open;        break;
  240.  
  241.             case kComponentCloseSelect :
  242.                     func = (ComponentFunctionUPP) Close;    break;
  243.  
  244.             case kComponentCanDoSelect :
  245.                 {
  246.                 short selector = *(short*) params->params;
  247.                 result = false;
  248.                 if ( selector==kWADescribeErrorSelect )
  249.                     result = true;
  250.                 break;
  251.                 }
  252.             case kComponentVersionSelect :
  253.                 result = kTaskVersion;
  254.                 break;
  255.             case kComponentRegisterSelect :
  256.                 result = false;                // false means "yes, please register me"    
  257.                 break;
  258.             case kComponentTargetSelect :
  259.             case kComponentUnregisterSelect :
  260.                 result = badComponentSelector;
  261.                 break;
  262.             }
  263.         if (func)
  264.             result = CallComponentFunction(params, func);
  265.         }
  266.     else                    // component specific request codes
  267.         {        
  268.         switch (params->what)
  269.             {
  270.             case    kWAGetCurrentSelect:
  271.                     func = (ComponentFunctionUPP) GetCurrent;        break;
  272.                     
  273.             case    kWASetCurrentSelect:
  274.                     func = (ComponentFunctionUPP) SetCurrent;        break;
  275.                     
  276.             case    kWADescribeSettingsSelect:
  277.                     func = (ComponentFunctionUPP) DescribeSettings;    break;
  278.             
  279.             case    kWACompareSettingSelect:
  280.                     func = (ComponentFunctionUPP) CompareSettings;    break;
  281.  
  282.             case    kWADescribeErrorSelect:
  283.                     func = (ComponentFunctionUPP) DescribeError;    break;
  284.  
  285.             default:
  286.                     func = (ComponentFunctionUPP) -1;
  287.             }
  288.  
  289.         if (func == (ComponentFunctionUPP) -1)
  290.             result = badComponentSelector;
  291.         else if (func)
  292.             result = CallComponentFunction(params, func);
  293.  
  294.         }
  295.  
  296. #if DEBUG
  297. {
  298.     char    str[256];
  299.  
  300.     sprintf( str, "what = %d, result = %ld", params->what, result );
  301.     DebugStr(c2pstr(str));
  302.         
  303. }
  304. #endif
  305.  
  306.     WAExitCodeResource();        
  307.     return result;
  308. }    // OTTask
  309. //-------------------------------------------------------------------------------------
  310.